package com.feicheng.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.task.Cache;
import com.feicheng.persistence.entity.Aclientbm;
import com.feicheng.persistence.entity.Akechenglx;
import com.feicheng.persistence.mapper.*;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/count")
public class CountController {

    @Autowired
    Cache cache;

    @Resource
    AclientbmMapper aclientbmMapper;

    @Resource
    AroomyyjlMapper aroomyyjlMapper;

    @Resource
    AhyjlMapper ahyjlMapper;

    @Resource
    ClientMapper clientMapper;

    @Resource
    AkechengMapper akechengMapper;

    @GetMapping("/amount")
    public ResultInfo getAmountTotal() {
        JSONObject param = new JSONObject();
        JSONObject bmAmount = aclientbmMapper.getBmPriceGroupByStu();
        param.put("bmAmount", bmAmount);
        JSONObject yyAmount = aroomyyjlMapper.getYyjlAmoutGroupByTflg();
        param.put("yyAmount", yyAmount);
        JSONObject hyAmount = ahyjlMapper.getHyjlAmountGroupByStu();
        param.put("hyAmount", hyAmount);
        JSONObject clientAmount = clientMapper.getTotalAmountByClient(null);
        param.put("clientAmount", clientAmount);
        JSONObject yjAmount = clientMapper.getTotalYjByClient(null);
        param.put("yjAmount", yjAmount);
        return ResultInfo.success(param);
    }

    @GetMapping("/kclx")
    public ResultInfo getKclxAmountByMonth() {
        JSONObject param = new JSONObject();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nowBefore1Month = now.minusMonths(1L);
        LocalDateTime nowBefore7Month = now.minusMonths(7L);
        QueryWrapper<Aclientbm> qw = new QueryWrapper<>();
        qw.ge("ct", nowBefore7Month);
        qw.le("ct", nowBefore1Month);
        List<JSONObject> list = akechengMapper.getKCtotalPricePerMonth(qw);
        List<JSONObject> moList = new ArrayList<>();
        list.forEach(jsonObject -> {
            if (!param.containsKey(jsonObject.getString("ym"))) {
                JSONObject mo = new JSONObject();
                param.put(jsonObject.getString("ym"), mo);
                moList.add(mo);
            }
            String kclxname = cache.getJSONObject("kclxs").getObject(jsonObject.getString("kclxid"), Akechenglx.class).getLxname();
            jsonObject.put("lxname", kclxname);
            JSONObject mo = param.getJSONObject(jsonObject.getString("ym"));
            mo.put(jsonObject.getString("revenue_rank"), jsonObject);
            mo.put("ym", jsonObject.getString("ym"));
        });
        param.put("molist", moList);
        return ResultInfo.success(param);
    }


}
